/**
 *	@author Juntalis
 */
//var arrServers = sUrl.split(/^TORIBASH 30\n/im);
function Lobby()
{
	var _servers = null;
	
	/**
	 * Lobby's servers.
	 * @return {Array} Array of Server objects
	 */
	this.Servers = function() {
		return _servers;
	}
	
	/**
	 * Populate the object with servers.
	 * @param {Function} cb Function to call on completion.
	 */
	this.Populate = function(cb) {
		_servers = new Array();
		var sckLobby = new socket();
		var strBuffer = "";
		sckLobby.init();
		sckLobby.handleReceives(
			function(event) {
				strBuffer += sckLobby.recv();
			}
		);
		sckLobby.handleClose(
			function(event) {
				if(!strBuffer) return;
				// Split it by server header.
				var arrServers = strBuffer.split(/\n?TORIBASH 30\n/);
				
				// Remove the first element(blank element).
				arrServers.splice(0,1);
				
				$.each(arrServers,
					function(index, sVal) {
						if (!sVal) return;
						var srvCurrent = new Server();
						
						var rgXp = /^INFO (\d{1,2});  ?(\d{1,3}) (\d{1,2})$/m;
						var sMatch = rgXp.exec(sVal);
						
						rgXp = /^INFO (\d{1,2});  ?(\d{1,3}) (\d{1,2})$/m;
						sMatch = rgXp.exec(sVal);
						if (sMatch != null) {
							srvCurrent.Players = parseInt(sMatch[1]);
							srvCurrent.MaxPlayers = parseInt(sMatch[2]);
						} else return;
						
						rgXp = /^SERVER 0; ([\d.]{7,15}):(\d{1,5}) (.*?)$/m;
						sMatch = rgXp.exec(sVal);
						if (sMatch != null) {
							srvCurrent.IP = sMatch[1];
							srvCurrent.Port = parseInt(sMatch[2]);
							srvCurrent.Name = sMatch[3];
						} else return;
						
						rgXp = /^DESC 0; ?(.*?)$/m;
						sMatch = rgXp.exec(sVal);
						if (sMatch != null) {
							srvCurrent.Description = sMatch[1];
						} else return;
						
						rgXp = /^CLIENTS \d; (.*?)$/m;
						sMatch = rgXp.exec(sVal);
						if (sMatch != null) {
							if (sMatch[1].length > 0) {
								srvCurrent.Clients = sMatch[1].split("\t");
							} else srvCurrent.Clients = [ "None" ];
						} else return;
						_servers.push(srvCurrent);
					}
				);
				if(cb) cb();
			}
		);
		sckLobby.connect("content.toribash.com", 22000);
	}
	
	this.Server = function(sname,callback)
	{
		if(!sname) return;
		if (_servers != null) {
			var srvTarget = null;
			$.each(_servers, function(index, value) {
				if (value.Name.toLowerCase() == sname.toLowerCase()) srvTarget = value;
			});
			if (srvTarget != null) {
				if (callback) callback(srvTarget.Name, srvTarget.IP, srvTarget.Port);
				return;
			}
		}
		var sckLobby = new socket();
		var strBuffer = "";
		sckLobby.init();
		sckLobby.handleConnection(
			function(event) {
				sckLobby.send("join " + sname);
			}
		);
		sckLobby.handleReceives(
			function(event) {
				strBuffer += sckLobby.recv();
			}
		);
		sckLobby.handleClose(
			function(event) {
				var rgXp = /^FORWARD 0;([\d.]{7,15}):(\d{1,5})$/m;
				var sMatch = rgXp.exec(strBuffer);
				if (sMatch != null)
					if (callback) callback(sname, sMatch[1], parseInt(sMatch[2]));
			}
		);
		sckLobby.connect("66.230.224.102", 22001);
	}
	
	function Server() {
		this.Name = "";
		this.Description = "";
		this.Players = 0;
		this.MaxPlayers = 0;
		this.IP = "";
		this.Port = 0;
		this.Clients = null;
	}
}

